package com.css.dzj.dzjap.common.util;

import com.css.dzj.dzjap.sys.user.entity.LoginUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Component
public class ServiceUtil {
    @Autowired
    HttpServletRequest request;

    public R getInfoFromBase(String url, Map<String, String> param, boolean useUserId, boolean usePost) {
        if (useUserId) {
            HttpSession session = request.getSession();
            if (session == null || (session.getAttribute("_USER_LOGIN_INFO_") == null)) {
                return R.error("session缺失");
            } else {
                LoginUser user = (LoginUser) session.getAttribute("_USER_LOGIN_INFO_");
                String userId;
                if (user != null) {
                    userId = user.getUserId();
                } else {
                    return R.error("user信息缺失");
                }
                if (param == null) {
                    param = new HashMap<>(1);
                }
                param.put("userId", userId.toString());
            }
        }
        String data;
        if (usePost) {
            data = HttpClientUtil.doPost(url, param);
        } else {
            data = HttpClientUtil.doGet(url, param);
        }
        return R.ok().put("data", data);
    }

    public R getUserInfoByRequest(HttpServletRequest request) {
        HttpSession loginSession = request.getSession();
        if (loginSession == null || (loginSession.getAttribute("_USER_LOGIN_INFO_") == null)) {
            return R.error("session缺失");
        }
        LoginUser userInfo = (LoginUser) loginSession.getAttribute("_USER_LOGIN_INFO_");
        return R.ok().put("data", userInfo);
    }


    /**
     * 获取登录名
     *
     * @param request
     * @return
     * @Author liyuan
     */
    public static String getLoginName(HttpServletRequest request) {
        HttpSession loginSession = request.getSession();
        if (loginSession == null || (loginSession.getAttribute("_USER_LOGIN_INFO_") == null)) {
            throw new RuntimeException("session缺失");
        }
        LoginUser userInfo = (LoginUser) loginSession.getAttribute("_USER_LOGIN_INFO_");
        String loginName = userInfo.getLoginName();
        if (StringUtils.isEmpty(loginName)) {
            throw new RuntimeException("用户名为空");
        }
        return loginName;
    }

    /**
     * 获取登录用户的组织名称
     *
     * @param request
     * @return
     * @Author liyuan
     */
    public static String getOrgName(HttpServletRequest request) {
        HttpSession loginSession = request.getSession();
        if (loginSession == null || (loginSession.getAttribute("_USER_LOGIN_INFO_") == null)) {
            throw new RuntimeException("session缺失");
        }
        LoginUser userInfo = (LoginUser) loginSession.getAttribute("_USER_LOGIN_INFO_");
        String orgName = userInfo.getOrgName();
        if (StringUtils.isEmpty(orgName)) {
            throw new RuntimeException("用户组织为空");
        }
        return orgName;
    }

    /**
     * 获取登录用户的ID
     * @param request
     * @return
     */
    public static String getUserId(HttpServletRequest request) {
        HttpSession loginSession = request.getSession();
        if (loginSession == null || (loginSession.getAttribute("_USER_LOGIN_INFO_") == null)) {
            throw new RuntimeException("session缺失");
        }
        LoginUser userInfo = (LoginUser) loginSession.getAttribute("_USER_LOGIN_INFO_");
        String userId = userInfo.getUserId().toString();
        if (StringUtils.isEmpty(userId)) {
            throw new RuntimeException("用户ID为空");
        }
        return userId;
    }
    /**
     * 获取登录用户关联的系统
     * @param request
     * @return
     */
    public static List<Map<String,Object>> getSysApp(HttpServletRequest request) {
        HttpSession loginSession = request.getSession();
        if (loginSession == null || (loginSession.getAttribute("_USER_LOGIN_INFO_") == null)) {
            throw new RuntimeException("session缺失");
        }
        LoginUser userInfo = (LoginUser) loginSession.getAttribute("_USER_LOGIN_INFO_");
        List<Map<String,Object>> sysApp = userInfo.getSysApp();
        if (StringUtils.isEmpty(sysApp)) {
            throw new RuntimeException("用户ID为空");
        }
        return sysApp;
    }
    /**
     * 获取登录Token
     *
     * @param request
     * @return
     */
    public static String getToken(HttpServletRequest request) {
        HttpSession loginSession = request.getSession();
        if (loginSession == null || (loginSession.getAttribute("_USER_LOGIN_INFO_") == null)) {
            throw new RuntimeException("session缺失");
        }
        LoginUser userInfo = (LoginUser) loginSession.getAttribute("_USER_LOGIN_INFO_");
        String accessToken = userInfo.getAccessToken();
        if (StringUtils.isEmpty(accessToken)) {
            throw new RuntimeException("Token为空");
        }
        return accessToken;
    }

}
